home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / sources.arc / CURVES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-19  |  7.0 KB  |  277 lines

  1. /* Program 
  2.   
  3.    Plots 5 segments, Q3->Q7
  4.    
  5.    Q3 uses points P0,P1,P2 and P3 (Pm-3, Pm-2, Pm-1, Pm   m=3)
  6.    Q4 uses points P1,P2,P3 and P4 (Pm-3, Pm-2, Pm-1, Pm   m=4)
  7.    ditto for Q5.& Q6 & Q7..
  8.  
  9.    Giving a total of 8 points needed. P0->P7, These points are
  10.    defined in Q[n]  where Q[0] = Point0, Q[1] = P1 etc..
  11.  
  12.  
  13.    Jeff Bilger              463-08-8302
  14.    Computer Graphics 441    Dr.Mc Cormick
  15.    Summer 1993              July 17,1993
  16.  
  17.  */
  18.  
  19. #include <linea.h>
  20. #include <osbind.h>
  21.  
  22. #define GREEN 2
  23. #define RED 1
  24. #define WHITE 0
  25. #define BLACK 3
  26.  
  27. struct PointDef
  28.  {
  29.   int x,y;         /* x,y position of Geometry Vector */
  30.   double B1,B2;    /* controls, a B1 & B2 for EACH control point! */
  31.  }G[8];            /* define 8 of em. 0-7 Hence 5 segments! */
  32.  
  33. double delta1,delta2,
  34.        delta3,delta4;   /* delta constants for segment */
  35. double Pm_3Coeff,     
  36.        Pm_2Coeff1,      /* The coeffs that stay constant and reoccur*/ 
  37.        Pm_2Coeff2,      /* in many equs to calculate x,y at t for */
  38.        Pm_2Coeff3,      /* a given Curve Qm.. Start at Q3 */
  39.        Pm_2Coeff4,
  40.        Pm_1Coeff1,
  41.        Pm_1Coeff2;
  42.  
  43. main()
  44. {
  45.  int n = 150,             /* the # of times to divide up 0 to 1 */
  46.      i,
  47.      curve=3;             /* What segment we're on! */
  48.  double delta = 1.0/ (double)n,  /* the delta value to inc t with */
  49.      x=0,y=0,        /* the x,y coords of functions N and Y*/
  50.      t=0;                        /* evaluated at t */
  51.  lineaport *myport;
  52.  
  53.    init();
  54.  
  55.     myport = a_init();
  56.  
  57.     myport -> plane0 = WHITE;
  58.     myport -> plane1 = WHITE;
  59.  printf("STARTING \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  60.  
  61. for(curve = 3; curve < 8; curve++)
  62. {
  63.  SetupSegment( curve );        /* sets up coeffs, and constants 
  64.                                   for a specified segment */
  65.  for( i=0;i<n;i++)
  66.    {
  67.       t += delta;              /* compute value of t */
  68.       SetupCurve( curve, t ,&x,&y);  /* compute values of */ 
  69.       Draw(x,y,RED);       /* Draw N function (x,y) in RED color */
  70.       
  71.     } /* end of t[0,1] interval */
  72.    t=0;    /* reset t */
  73.  }   /* end of curve # */
  74. }
  75.  
  76.  
  77.  
  78. /************************************************/
  79. /* calculates value if x,y using 
  80.    precalculated coeffs and deltas, plus t 
  81.  
  82.    G[0] is point P0
  83.    G[1] is point P1
  84.    G[2] is point P2
  85.   
  86.    so when m =3  ( on 1st segment curve Q3 )
  87.    so G[m-3] ===> G[0]  point0
  88.       G[m-2] ===> G[1]  point1
  89.       G[m-1] ===> G[2]  point2
  90.       G[m]   ===> G[3]  point3
  91.   
  92.      Which are the 4 points that define Q3!!!
  93.  
  94. */
  95.  
  96. SetupCurve( m , t , x,y )
  97. int m;         /* holds curve we're on Q3,4 or 5 */
  98. double t,      /* current place */
  99.        *x,     /* returns this */
  100.        *y;     /* and this */
  101. {
  102.  double t2,t3;
  103.  
  104.        t2 = t*t;        /* compute t^2 and t^3 */
  105.        t3 = t2 * t;
  106.  
  107. /** may wish to compute mass of this once and apply to g[n].x and g[n].y
  108.     values... **/
  109.  
  110.  
  111.   *x = ((1.0/delta1) * ( -2.0*Pm_3Coeff*t3  + 6.0*Pm_3Coeff*t2  - 6.0*Pm_3Coeff*t  + 2.0*Pm_3Coeff) * (double)G[m-3].x)
  112.       +((1.0/delta2) * (  2.0*Pm_2Coeff1*t3 - 3.0*Pm_2Coeff2*t2 + 6.0*Pm_2Coeff3*t + Pm_2Coeff4)    * (double)G[m-2].x)
  113.       +((1.0/delta3) * ( -2.0*Pm_1Coeff1*t3 + 3.0*Pm_1Coeff2*t2 + 6.0*G[m-1].B1*t  + 2.0)           * (double)G[m-1].x)
  114.       +((1.0/delta4) * (  2.0*t3 ) * (double)G[m].x);
  115.  
  116.   *y = ((1.0/delta1) * ( -2.0*Pm_3Coeff*t3  + 6.0*Pm_3Coeff*t2  - 6.0*Pm_3Coeff*t  + 2.0*Pm_3Coeff) * (double)G[m-3].y)
  117.       +((1.0/delta2) * (  2.0*Pm_2Coeff1*t3 - 3.0*Pm_2Coeff2*t2 + 6.0*Pm_2Coeff3*t + Pm_2Coeff4)    * (double)G[m-2].y)
  118.       +((1.0/delta3) * ( -2.0*Pm_1Coeff1*t3 + 3.0*Pm_1Coeff2*t2 + 6.0*G[m-1].B1*t  + 2.0)           * (double)G[m-1].y)
  119.       +((1.0/delta4) * (  2.0*t3 ) * (double)G[m].y);
  120.  
  121.      
  122.  
  123. /*
  124. printf("\n\n**** Stat for Calculate x,y ****\n");
  125. printf("t  >%f    t2  >%f   t3  >%f \n",t,t2,t3);
  126. printf("x  >%f    y   >%f\n",*x,*y);
  127. printf("********* End stat  *********\n\n");
  128.  
  129. */
  130.  
  131. }
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. /********************************/
  139. /* m holds curve we're on : either 3,4,5,6 or 7 
  140.  
  141.  
  142.  
  143.  
  144.   Sets up deltas and constant,reeoccuring coeffs:
  145.   
  146.        Pm_3Coeff,     
  147.        Pm_2Coeff1,       The coeffs that stay constant and reoccur 
  148.        Pm_2Coeff2,       in many equs to calculate x,y at t for 
  149.        Pm_2Coeff3,       a given Curve Qm.. Start at Q3 
  150.        Pm_2Coeff4,
  151.        Pm_1Coeff1,
  152.        Pm_2Coeff2;
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. */
  160. SetupSegment( m )
  161. int m;
  162. {
  163.  double hold1,hold2,hold3,hold4;
  164.  
  165.   hold1 = G[m-3].B1*G[m-3].B1*G[m-3].B1;  
  166.   hold2 = G[m-2].B1*G[m-2].B1*G[m-2].B1;
  167.   hold3 = G[m-2].B1*G[m-2].B1;
  168.   hold4 = G[m-1].B1*G[m-1].B1;
  169.  
  170.   delta1 = G[m-3].B2 + 2.0*hold1 + 4.0*(G[m-3].B1*G[m-3].B1) + 4.0*G[m-3].B1 + 2.0;
  171.   
  172.   delta2 = G[m-2].B2 + 2.0*hold2 + 4.0*hold3 + 4.0*G[m-2].B1 + 2.0;
  173.  
  174.   delta3 = G[m-1].B2 + 2.0*(hold4 * G[m-1].B1) + 4.0*hold4 + 4.0*G[m-1].B1 + 2.0;
  175.  
  176.   delta4 = G[m].B2 + 2.0*(G[m].B1*G[m].B1*G[m].B1) + 4.0*(G[m].B1*G[m].B1) + 4.0*G[m].B1 + 2.0;
  177.  
  178.  
  179.   Pm_3Coeff = hold1;
  180.   Pm_2Coeff1= G[m-2].B2 + hold2     + hold3      + G[m-2].B1;
  181.   Pm_2Coeff2= G[m-2].B2 + 2.0*hold2 + 2.0*hold3;
  182.   Pm_2Coeff3= hold2 - G[m-2].B1;
  183.   Pm_2Coeff4= G[m-2].B2 + 4.0*(hold3 + G[m-2].B1);
  184.   Pm_1Coeff1= G[m-1].B2 + hold4 + G[m-1].B1 + 1.0;
  185.   Pm_1Coeff2= G[m-1].B2 + 2.0 * hold4; 
  186.  
  187. /*
  188.   printf("Stats after SetUpSegment\n");
  189.   printf("delta1:%f   delta2:%f   delta3:%f   delta4:%f  \n",delta1,delta2,delta3,delta4);
  190.   printf("\nCoeffs\n");
  191.   printf("Pm_3   >%f\n",hold1);
  192.   printf("Pm_2:1 >%f  Pm_2:2 >%f  Pm_2:3 >%f  Pm_2:4  >%f\n",Pm_2Coeff1,Pm_2Coeff2,Pm_2Coeff3,Pm_2Coeff4);
  193.   printf("Pm_1:1 >%f  Pm_1:2 >%f\n",Pm_1Coeff1,Pm_1Coeff2);
  194.   printf("******* done with this stat *********");  
  195. */
  196. }
  197. /********************************/
  198. /* Displays a pixel on the screen given x,y and a color.
  199. */
  200. Draw(x,y,color)
  201. double x,y;                /* the coords to plot at */
  202. int color;                 /* the color of the pixel */
  203.  
  204. {
  205.  int x1,y1;                /* local int. vars */
  206.  char a;                   
  207.  
  208.  x1 = (int) x;             /* typecast to integer */
  209.  y1 = (int) y;
  210.  
  211. /*
  212.  printf("doubles: x:%f y:%f\nints x:%d y:%d\n",x,y,x1,y1);
  213. */             
  214. a_putpixel(x1, y1, color);
  215. a=Bconin(2);
  216. }
  217.  
  218. /*******************************************/
  219. /*
  220.    init()
  221.  
  222. inits the G[n] values 
  223. G[0] to G[7]   8 points
  224.  
  225.  
  226. */
  227. init()
  228. {
  229.  int p;
  230.  
  231.  G[0].x  = 50;
  232.  G[0].y  = 90;
  233.  G[0].B1 = 10.5;
  234.  G[0].B2 = 12.1;
  235.  
  236.  G[1].x  = 50;
  237.  G[1].y  = 40;
  238.  G[1].B1 = 20.1;
  239.  G[1].B2 = 15.1;
  240.  
  241.  G[2].x  = 80;
  242.  G[2].y  = 40;
  243.  G[2].B1 = 9.1;
  244.  G[2].B2 = 6.0;
  245.  
  246.  G[3].x  = 80;
  247.  G[3].y  = 90;
  248.  G[3].B1 = 10.1;
  249.  G[3].B2 = 50.0;
  250.  
  251.  G[4].x  = 130; 
  252.  G[4].y  = 90;
  253.  G[4].B1 = 9.3;
  254.  G[4].B2 = 999.8;
  255.  
  256.  G[5].x  = 130;
  257.  G[5].y  = 40;
  258.  G[5].B1 = 5.1;
  259.  G[5].B2 = 888.5;
  260.  
  261.  G[6].x  = 300;
  262.  G[6].y  = 40;
  263.  G[6].B1 = 5.1;
  264.  G[6].B2 = 888.5;
  265.  
  266.  
  267.  G[7].x  = 300;
  268.  G[7].y  = 90;
  269.  G[7].B1 = 888.1;
  270.  G[7].B2 = 88.5;
  271.  
  272.  
  273.  
  274.            
  275. for(p=0;p<6;p++)
  276.  printf("G[%d]   x >%d  y >%d  b1 >%f  b2  >%f\n",p,G[p].x,G[p].y,G[p].B1,G[p].B2);
  277. }